﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 花店管理系统.Department
{
    public partial class Insert_department : Form
    {
        public Insert_department()
        {
            InitializeComponent();
        }
        SqlDataAdapter daDeparment;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from Tdepartment";
            daDeparment = new SqlDataAdapter(str, DB.cn);
            daDeparment.Fill(ds, "department_info");
            DB.cn.Close();
        }
        void showAll()
        {
            DataView dvDepartment = new DataView(ds.Tables["department_info"]);
            dgvDepartment.DataSource = dvDepartment;
            dgvDepartment.Columns[3].Width = 500;
        }
        void dgvHead()
        {
            this.dgvDepartment.Columns[0].HeaderText = "部门编号";
            this.dgvDepartment.Columns[1].HeaderText = "部门名称";
            this.dgvDepartment.Columns[2].HeaderText = "部门经理";
            this.dgvDepartment.Columns[3].HeaderText = "部门描述";

        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void Insert_department_Load(object sender, EventArgs e)
        {
            init();
            showAll();
            dgvHead();
        }

        private void dgvDepartment_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            bool flowControl = NewMethods();
            if (!flowControl)
            {
                return;
            }

            bool NewMethods()
            {
                if (txtId.Text == "" || txtName.Text == "")
                {
                    MessageBox.Show("部门编号或部门名称不能为空");

                }
                else
                {
                    DB.GetCn();
                    DialogResult dr = MessageBox.Show("你确定要添加吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        string s_id = "select * from Tdepartment where D_id='" + txtId.Text + "'";
                        DataTable dtl = DB.GetDataSet(s_id);
                        if (dtl.Rows.Count > 0)
                        {
                            MessageBox.Show("此部门编号已存在，请重新输入");
                        }
                        else
                        {
                            string s_name = "select * from Tdepartment where D_name='" + txtName.Text + "'";
                            DataTable dt2 = DB.GetDataSet(s_name);
                            if (dt2.Rows.Count > 0)
                            {
                                MessageBox.Show("此部门名称已存在，请重新输入");
                            }
                            else
                            {
                                DataRow DepRow = ds.Tables["department_info"].NewRow();
                                DepRow["D_id"] = txtId.Text.Trim();
                                DepRow["D_name"] = txtName.Text.Trim();
                                DepRow["D_jinli"] = txtManager.Text.Trim();
                                DepRow["D_miaosu"] = txtDescn.Text.Trim();
                                ds.Tables["department_info"].Rows.Add(DepRow);

                            }
                        }
                    }
                    else
                    {
                        return false;
                    }
                    //使用存储过程添加日记
                    SqlCommand cmd = new SqlCommand("add_log", DB.cn);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add(new SqlParameter("tname", SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("L_type", SqlDbType.NVarChar));
                    cmd.Parameters.Add(new SqlParameter("L_date", SqlDbType.DateTime));
                    cmd.Parameters.Add(new SqlParameter("L_table", SqlDbType.NVarChar));

                    cmd.Parameters["tname"].Value = Login.username;
                    cmd.Parameters["L_type"].Value = "添加";
                    cmd.Parameters["L_date"].Value = DateTime.Now;
                    cmd.Parameters["L_table"].Value = "department表";

                    try
                    {
                        SqlCommandBuilder dbDepartment = new SqlCommandBuilder(daDeparment);
                        daDeparment.Update(ds, "department_info");
                        cmd.ExecuteNonQuery();
                    }
                    catch (SqlException ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

                return true;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();

        }
    }
}
